﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data.SqlClient;
using System.Net.Configuration;

namespace MvcApplication1
{
    /// <summary>
    /// dapper(单例模式)用于创建一个访问数据的对象
    /// </summary>
    public class DapperHelper
    {
        /// <summary>
        /// 定义一个私有的静态变量用来存储类的对象
        /// </summary>
        private static DapperHelper _dapperHelper = null;
        /// <summary>
        /// 定义一个私有的只读字符串
        /// </summary>
        private readonly string _connString = "";
        /// <summary>
        /// 定义一个私有的构造函数
        /// </summary>
        private DapperHelper() 
        {
            //类被实例化时将数据库连接字符串传递给_connString
            _connString = "Data Source=.;Initial Catalog=ExamKu;Integrated Security=True";
        }
        /// <summary>
        /// 应用程序接口
        /// </summary>
        /// <returns></returns>
        public static DapperHelper Instance() 
        {
            if (_dapperHelper == null) 
            {
                _dapperHelper = new DapperHelper();
            }
            return _dapperHelper;
        }

        /// <summary>
        /// 定义公共的连接数据库实例化sqlconnection对象
        /// </summary>
        /// <returns></returns>
        public SqlConnection GetSqlconnection() 
        {
            var conn = new SqlConnection(_connString);
            conn.Open();
            return conn;
            
        }

    }
}
